package com.bsoft.gol.hbs.dao;

import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import org.atag.dao.ext.HibernateExtendDAO;
import org.atag.dao.util.BaseDaoImpl;
import pcn.consultation.OnlineConsultPrice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SsdevDao(serviceId = "onlineConsultPriceDAOService")
public abstract class OnlineConsultPriceDAO extends BaseDaoImpl<OnlineConsultPrice> {
    public OnlineConsultPriceDAO() {
        this.setKeyField("id");
    }

    @DAOMethod(sql = " from OnlineConsultPrice where configId=:configId and consultType=:consultType")
    public abstract List<OnlineConsultPrice> queryOnlineConsultPriceByConsultType(@DAOParam("configId") int configId, @DAOParam("consultType") String consultType);


    @DAOMethod(sql = " from OnlineConsultPrice where configId=:configId and consultType=:consultType and doctorLevel in:doctorLevelList  ")
    public abstract List<OnlineConsultPrice> queryByDoctorLevelList(@DAOParam("configId") int configId, @DAOParam("consultType") String consultType, @DAOParam("doctorLevelList") List<String> doctorLevelList);


    @DAOMethod(sql = " from OnlineConsultPrice where configId=:configId and consultType=:consultType and doctorLevel='0' ")
    public abstract OnlineConsultPrice getOnlineConsultPriceByLevel(@DAOParam("configId")int configId, @DAOParam("consultType")String consultType);

    @RpcService
    @DAOMethod(sql = " from OnlineConsultPrice where configId=:configId")
    public abstract List<OnlineConsultPrice> queryOnlineConsultPriceByConfigId(@DAOParam("configId")int configId);

    @RpcService
    @DAOMethod(sql = " from OnlineConsultPrice where configId=:configId and doctorLevel=:doctorLevel")
    public abstract List<OnlineConsultPrice> queryOnlineConsultPriceByDoctorLevel(@DAOParam("configId")int configId,@DAOParam("doctorLevel")String doctorLevel);

    public List<Map<String,Object>> queryOnlineConsultConfigByParams(final String orgId,final String consulttype, final String doctorlevel) {
        Map<String, Object> params = new HashMap<>();
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT a.*,b.org_id ");
        sql.append(" from online_consult_price a, online_consult_config b where a.config_id=b.id  ");
        sql.append(" and a.status='1' and b.status='1' ");

        if (CommonUtils.isNotEmpty(consulttype)) {
            sql.append(" and  a.consult_type= :consulttype");
            params.put("consulttype", consulttype);
        }
        if (CommonUtils.isNotEmpty(doctorlevel)) {
            sql.append(" AND a.doctor_level = :doctorlevel");
            params.put("doctorlevel", doctorlevel);
        }
        if (CommonUtils.isNotEmpty(orgId)) {
            sql.append(" AND b.org_id = :orgId");
            params.put("orgId", orgId);
        }

        return super.findListBySql(sql.toString(), params, Map.class);
    }

    @DAOMethod(sql = " from OnlineConsultPrice where doctorLevel='0' and status=1")
    public abstract List<OnlineConsultPrice> queryDeptConsultPrice();
}
